﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webapp4U;
using webapp4U.BOL;
using Labwell;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class UserControl_ControlRegisterMember : System.Web.UI.UserControl
{
    string error = "";
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //uscMsgBox1.MsgBoxAnswered += MessageAnswered;
        if (!IsPostBack)
        {           
            uscMsgBox1.Title = ResourceManager.GetString("uscMsgBox_Title");
            uscMsgBox1.OKText = ResourceManager.GetString("uscMsgBox_btnOK");
            uscMsgBox1.CancelText = ResourceManager.GetString("uscMsgBox_btnCancel");
            uscMsgBox1.CloseText = ResourceManager.GetString("uscMsgBox_btnClose");
            
        }
    }
    
    private bool ChecktxtValue()
    {
        bool flag = true;
        if (txtSUName.Value == "")
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("Register_FirstName_error"), uscMsgBox.enmMessageType.Error);
        }
        if (txtSUNick.Value.Trim() == "")
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("Register_NickName_error"), uscMsgBox.enmMessageType.Error);
        }
        if (txtSUEmail.Value == "")
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("Register_Email_error"), uscMsgBox.enmMessageType.Error);
        }
        else
        { 
            string pattern = @"^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$" ;
            Regex reg = new Regex(pattern);
            if (!reg.IsMatch(txtSUEmail.Value))
            {
                flag = false;
                uscMsgBox1.AddMessage(ResourceManager.GetString("Register_Email_error"), uscMsgBox.enmMessageType.Error);
            }
        }

        if (txtSUPass.Value=="")
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("Register_Password_error"), uscMsgBox.enmMessageType.Error);
        }
        if (txtSURePass.Value != txtSUPass.Value)
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("Register_ConfirmPassword_error"), uscMsgBox.enmMessageType.Error);
        }
        if (ddlDate.Value == "0" || ddlMonth.Value == "0" || ddlYear.Value == "0")
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("Register_Birthday_error"), uscMsgBox.enmMessageType.Error);
        }
        if (txtCapcha.Value != Session["CaptchaImageText"].ToString())
        {
            flag = false;
            uscMsgBox1.AddMessage(ResourceManager.GetString("ConfirmationCode_error"), uscMsgBox.enmMessageType.Error);
        }
        
        return flag;

    }
    protected void btnAccept_Click(object sender, EventArgs e)
    {
        if (cbAccept.Checked)
        {
            if (ChecktxtValue())
            {
                string Pass = txtSUPass.Value;
                string Birthday = ddlDate.Value + "/" + ddlMonth.Value + "/" + ddlYear.Value;

                if (RegisterMemberBOL.InsertMemberRegister(ref error, txtSUName.Value, txtSUNick.Value, txtSUEmail.Value, Pass, cbMale.Checked, Birthday, cbNote.Checked))
                {
                    uscMsgBox1.AddMessage(ResourceManager.GetString("Register_CreatedSuccessfully"), uscMsgBox.enmMessageType.Success);
                    //uscMsgBox1.AddMessage(ResourceManager.GetString("Register_CreatedSuccessfully"), uscMsgBox.enmMessageType.Success, true, false, "Redirect");
                }
                else
                {
                    uscMsgBox1.AddMessage(ResourceManager.GetString("Register_Created_Failed"), uscMsgBox.enmMessageType.Error);
                }
            }
        }
        
    }
    public void MessageAnswered(object sender, uscMsgBox.MsgBoxEventArgs e)
    {
        if (e.Args == "Redirect")
        {
            if (e.Answer == uscMsgBox.enmAnswer.OK)
            {
                Response.Redirect(Globals.ApplicationPath + "Page/RegisterMember/Default.aspx?Success=Member");
            }
        }
    }
      
    protected void cbMale_CheckedChanged(object sender, EventArgs e)
    {
        cbFemale.Checked = !cbMale.Checked;
    }
    protected void cbFemale_CheckedChanged(object sender, EventArgs e)
    {
        cbMale.Checked = !cbFemale.Checked;
    }
}